﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using ERPStore.Web.Html;
using ERPStore.Extensions;
using ERPStore.Web.Extensions;

namespace ERPStore.Web.Controllers
{
	public class SalesController : StoreController
	{
		public SalesController(
			ERPStore.Services.ISalesService saleService,
			ERPStore.Services.ICatalogService catalogService
			)
		{
			this.SalesService = saleService;
			this.CatalogService = catalogService;
		}

		#region Properties

		protected ERPStore.Services.ISalesService SalesService { get; private set; }
		protected ERPStore.Services.ICatalogService CatalogService { get; private set; }

		#endregion

		#region Render Partial

		// [Authorize(Roles = "customer")]
		public ActionResult ShowOrderWorkflow(string viewName, ERPStore.Models.Order order)
		{
			var workflowList = SalesService.GetWorkflow(order);
			SetupUrl(workflowList);
			ViewData.Model = workflowList;
            return PartialView(viewName);
		}

		// [Authorize(Roles = "customer")]
		public ActionResult ShowQuoteWorkflow(string viewName, ERPStore.Models.Quote quote)
		{
			var workflowList = SalesService.GetWorkflow(quote);
			SetupUrl(workflowList);
			ViewData.Model = workflowList;
            return PartialView(viewName);
		}

		// [Authorize(Roles = "customer")]
		public ActionResult ShowInvoiceWorkflow(string viewName, ERPStore.Models.Invoice invoice)
		{
			var workflowList = SalesService.GetWorkflow(invoice);
			SetupUrl(workflowList);
			ViewData.Model = workflowList;
			return PartialView(viewName);
		}

		public ActionResult ShowVendor(string viewName, ERPStore.Models.Quote quote, ERPStore.Models.Order order)
		{
			if (User.Identity.IsAuthenticated)
			{
				if (quote != null)
				{
					ViewData.Model = quote.Vendor;
				}
				else if (order != null)
				{
					ViewData.Model = order.Vendor;
				}
				else
				{
					ViewData.Model = User.CurrentUser.Vendor ?? User.CurrentUser.Corporate.Vendor;
				}
			}
			else
			{
				ViewData.Model = ERPStore.GlobalConfiguration.Configuration.Settings.Contact.DefaultVendorId;
			}
			return PartialView(viewName);
		}

		// For bot in get
		public ActionResult ShowProductPriceHistory()
		{
			return new EmptyResult();
		}

		[HttpPost]
		public ActionResult ShowProductPriceHistory(string productCode, string viewName)
		{
			var priceHistory = new ERPStore.Models.PriceHistory();
			var product = CatalogService.GetProductByCode(productCode);
			if (product == null)
			{
				return new EmptyResult();
			}
			priceHistory.Product = product;
			ViewData.Model = priceHistory;
			var customer = User.CurrentUser;
			if (customer != null)
			{
				priceHistory = SalesService.GetPriceHistory(product, customer);
			}
			SalesService.ApplyBestPrice(priceHistory.Product, customer);
			return PartialView(viewName);
		}

		#endregion

		private void SetupUrl(ERPStore.Models.WorkflowList list)
		{
			var urlHelper = new UrlHelper(this.ControllerContext.RequestContext);
			foreach (var item in list)
			{
				item.DownloadUrl = urlHelper.DownloadDocumentHref(item);
				switch (item.Type)
				{
					case ERPStore.Models.SaleDocumentType.Order:
						item.Url = urlHelper.OrderHref(item.Code).ToHtmlString();
						break;
					case ERPStore.Models.SaleDocumentType.Quote:
						item.Url = urlHelper.QuoteHref(item.Code);
						break;
					case ERPStore.Models.SaleDocumentType.Delivery:
						break;
					case ERPStore.Models.SaleDocumentType.Invoice:
						item.Url = urlHelper.InvoiceHref(item.Code);
						break;
					default:
						break;
				}
			}
		}
	}
}
